#This script, when complete, will be one of the most important ones for the entire semester.
library(ExPosition)

#run a PCA on the French social class data set. You should make sure the data are centered and scaled.
#social.res <- epPCA(???)
data(french.social)
social.res <- epPCA(french.social$data)


#Find the singular values and save them below
#sing.vals <- ???
sing.vals <- social.res$ExPosition.Data$pdq$Dv


#Find the factor scores for the first 5 rows (1-5; observations) and first 2 columns (1-2; components) from the output. There are several hints below.
#?epPCA
#?corePCA
#row.fs <- social.res$ExPosition.Data$ ???
row.fs <- social.res$ExPosition.Data$fi


#for the following, you can use the same exact code we made for the t-statistic from class with just 1 little change (outlined below).
#compute a t statistic on each element in the row.fs table. There is a slight change, though: the standard deviation will be replaced by the corresponding singular value. For example:
	#I want to test the third observation on the second component:
	#row.fs[3,2]
	#Where the apply(...,sd) line from before is replaced by:
	#sing.vals[2]
	###For component 1:
	#row.fs[3,1]
	#Where the apply(...,sd) line from before is replaced by:
	#sing.vals[1]
	# score = factor score; population mean = 0; sd = singular value; and still use sqrt(N) in your t formula.
	
	#do this for all 10 values (5 rows from component 1; 5 rows from component 2) in your row.fs table.
	#be sure to save your values into the table below:
row.fs.t.values <- matrix(0,5,2)
	#row.fs.t.values[3,2] should be the t-value from above.	
row.fs.t.values[1,1] <- row.fs[1,1] / (sing.vals[1]/sqrt(nrow(social.res$ExPosition.Data$fi)))
row.fs.t.values[2,1] <- row.fs[2,1]/ (sing.vals[1]/sqrt(nrow(social.res$ExPosition.Data$fi)))
row.fs.t.values[3,1] <- row.fs[3,1]/ (sing.vals[1]/sqrt(nrow(social.res$ExPosition.Data$fi)))
row.fs.t.values[4,1] <- row.fs[4,1]/ (sing.vals[1]/sqrt(nrow(social.res$ExPosition.Data$fi)))
row.fs.t.values[5,1] <- row.fs[5,1]/ (sing.vals[1]/sqrt(nrow(social.res$ExPosition.Data$fi)))

row.fs.t.values[1,2] <- row.fs[1,2]/ (sing.vals[2]/sqrt(nrow(social.res$ExPosition.Data$fi)))
row.fs.t.values[2,2] <- row.fs[2,2]/ (sing.vals[2]/sqrt(nrow(social.res$ExPosition.Data$fi)))
row.fs.t.values[3,2] <- row.fs[3,2]/ (sing.vals[2]/sqrt(nrow(social.res$ExPosition.Data$fi)))
row.fs.t.values[4,2] <- row.fs[4,2]/ (sing.vals[2]/sqrt(nrow(social.res$ExPosition.Data$fi)))
row.fs.t.values[5,2] <- row.fs[5,2]/ (sing.vals[2]/sqrt(nrow(social.res$ExPosition.Data$fi)))


#Now do a t-test! Remember, t is like z. Anything greater than an absolute value of 2 is significant. You'll need the following items:
	#abs()
	# > (or <)
	#examples:
print(10 < 2)
print(2 > 10)
print(2 < 10)
print(-10 < 10)	
print(abs(-10) < 10)
	#save items in the following table:
row.fs.t.tests <- matrix(0,5,2)	
row.fs.t.tests <- abs(row.fs.t.values) > 2
#your final table will not have numbers, and should be filled with TRUE or FALSE, if done correctly.